package com.smu.recognize.controller;

import com.smu.recognize.model.Admin;
import com.smu.recognize.model.Apply;
import com.smu.recognize.model.common.JsonReturn;
import com.smu.recognize.mqtt.MqttPushClient;
import com.smu.recognize.service.ApplyService;
import com.smu.recognize.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.smu.recognize.controller.adminController.CommonAction.getUserFromSession;

@RestController
@Slf4j
public class ApplyController {
    List<Apply> list=new ArrayList<>();
    JsonReturn jsonReturn=new JsonReturn(true);

    @Autowired
    private ApplyService service;
    @Autowired
    private UserService userService;

    /**
     * 插入申请记录
     * @param apply
     * @return
     */
    @RequestMapping("/insertApply")
    public boolean insert(Apply apply){

        /*
        *
        * 此处加一个消息推送
        * */
        int id=userService.findIdByUserName(apply.getApprovedby());
        apply.setApprovedbyId(id);
        boolean flag= false;
        try {
            flag = service.addApply(apply);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        log.info(apply.toString());
        MqttPushClient client=MqttPushClient.getInstance();
        client.workPublish(apply.getName());
        return flag;
    }

    /**
     * 根据申请状态查询申请记录
     * @param a_status
     * @return
     */
    @RequestMapping("/findByStatus")
    public List<Apply> findByStatus(int a_status){
        list=service.findByStatus(a_status);
        log.info(list.toString());
        return list;
    }

    /**
     * 根据用户id查询申请记录
     * @param user_id
     * @return
     */
    @RequestMapping("/findByUser_id")
    public List<Apply> findByUser_id(int user_id){
        list=service.findByUser_id(user_id);
        log.info(list.toString());
        return list;
    }

    /**
     * 根据审批人id和审批状态查询申请记录
     * @param approvedById
     * @param a_status
     * @return
     */
    @RequestMapping("/findApplyByApprovedbyId")
    public List<Apply> findApplyByApprovedbyId(int approvedById,int a_status){
        //int user_id=userService.findIdByUserName(userName);
        list=service.findApplyByApprovedbyId(approvedById,a_status);
        return list;
    }
    //审批接口
    @RequestMapping("/approve")
    public List<Apply> approve(int app_id, int a_status, int user_id){
        service.updateApply(app_id,a_status);
        log.info("审批成功");
        return service.findApplyByApprovedbyId(user_id,0);
    }

    //web接口，全部申请
    @RequestMapping("/apply/all")
    public JsonReturn findApply(HttpServletRequest request, HttpServletResponse response){
        Admin user = getUserFromSession(request);
        if (user == null) {
            try {
                response.sendRedirect(request.getContextPath() + ""); // 重定向至登录页
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else
            list=service.findAllApply();
            jsonReturn.setCode(0);
            jsonReturn.setData(list);
            return jsonReturn;

    }
    @RequestMapping("/web/approve")
    public JsonReturn webApprove(Integer app_id,int a_status){
        boolean flag=service.updateApply(app_id,a_status);
        log.info("Web审批成功");
        return new JsonReturn(flag);
    }


}
